#include <iostream>
#include "BinaryLinkTree.hpp"

// 比较两个字符的回调函数
int CompareChar(char a, char b) 
{
    return a - b;
}

// 显示字符的回调函数
void ShowChar(char a) 
{
    std::cout << a << " ";
}

// 根据索引查找字符的回调函数
int SearchCharByIndex(char,int index) 
{
    static int currentIndex = -1;
    currentIndex++;
    if (currentIndex == index) {
        currentIndex = -1; // Reset index after finding the element
        return 0;
    }
    return currentIndex < index ? -1 : 1;
}

int main() 
{
    BinaryLinkTree<char> tree;
    tree.SetCallbackCompareID(CompareChar);
    tree.SetCallbackShowElement(ShowChar);
    tree.SetCallbackSearchByIndex(SearchCharByIndex);

    // char a = 'b', b = 'a', c = 'd', d = 'c', e = 'e';
    
    // 插入字符
    tree.Insert('a');
    tree.Insert('b');
    tree.Insert('c');
    tree.Insert('d');
    tree.Insert('e');

    std::cout << "树的前序遍历: ";
    tree.show();

    // 查找字符
    char searchValue = 'd';
    void* result = tree.Find(searchValue);
    if (result) {
        std::cout << "找到元素: " << *(char*)result << std::endl;
    } else {
        std::cout << "未找到元素: " << searchValue << std::endl;
    }

    // 根据索引查找字符
    int index = 2;
    result = tree.FindByIndex(index);
    if (result) {
        std::cout << "索引 " << index << " 对应的元素: " << *(char*)result << std::endl;
    } else {
        std::cout << "索引 " << index << " 无对应的元素" << std::endl;
    }

    // 删除字符
    tree.Remove('b');
    std::cout << "删除元素 " << "b" << " 后的树的前序遍历: ";
    tree.show();

    return 0;
}